package com.yt.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class JDAccessToken {


    public static void main(String[] args) {
        getToken();
    }

    public static Map<String, String> getToken() {
        String client_secret = "ONepKgDLS6e7qfqPSb62";
        String grant_type = "access_token";
        String client_id = "COoRZcg0CSKkG4vJYq7N";
        String timestamp = DateUtils.getCurrentDateTimeStr();
        String username = "GF管理";
        String password = Md5Utils.code("jd123456");
        Map map = new HashMap();
        map.put("grant_type", grant_type);
        map.put("client_id", client_id);
        map.put("timestamp", timestamp);
        map.put("username", username);
        map.put("password", password);
        map.put("sign", Md5Utils.code(client_secret + timestamp + client_id + username + password + grant_type + client_secret).toUpperCase());

        String str = HttpUtils.doPost("https://api-iop.jd.com/oauth2/accessToken", map);
        System.out.println(str + "---------------");
        JSONObject jsonObject = JSONObject.parseObject(str);
        String resultCode = jsonObject.getString("resultCode");
        Map resultmap = new HashMap();
        if (resultCode.equals("0000")) {
            String access_token = jsonObject.getJSONObject("result").getString("access_token");
            String expires_in = jsonObject.getJSONObject("result").getString("expires_in");
            System.out.println("access_token====" + access_token);

            resultmap.put("token", access_token);
            resultmap.put("expires", expires_in);
        }
        return resultmap;
    }


    public static Map<String, String> getJDsku(String token, String pageNum, int pageSize, String offset) {

        Map skuparam = new HashMap();
        skuparam.put("token", token);
        skuparam.put("pageNum", pageNum);
        skuparam.put("pageSize", pageSize);
        skuparam.put("offset", offset);

        //获取京东商品SKU
        String str = HttpUtils.doPost("https://api-iop.jd.com/api/product/querySkuByPage", skuparam);
        JSONObject jsonObject = JSONObject.parseObject(str);
        String resultCode = jsonObject.getString("resultCode");
        Map resultmap = new HashMap();
        if (resultCode.equals("0000")) {
            String skus = jsonObject.getJSONObject("result").getString("skus");
            offset = jsonObject.getJSONObject("result").getString("offset");
            resultmap.put("skus", skus);
            resultmap.put("offset", offset);
        }
        return resultmap;
    }


}
